<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2020 All rights reserved.
// +----------------------------------------------------------------------
// | User: Simony <1362229026@qq.com>
// | wechat: Shen86579595
// +----------------------------------------------------------------------

namespace app\common\model\content;

use app\common\model\Base;

use think\db;

class Content extends Base
{


    protected $name = 'content';

    public $types = [
        0 => '系统广播',
    ];


    public function initialize()
    {
        $contentTypeList = model('common/content/ContentType')->getCol('id,name', ['pid' => 0]);
        $this->types = array_merge($this->types, $contentTypeList);

    }

    public function file()
    {
        return $this->hasMany('app\common\model\member\File', 'table_id', 'id');
    }


    public function member()
    {
        return $this->hasOne('app\common\model\member\Member', 'id', 'member_id');
    }

    public function category()
    {
        return $this->hasOne('app\common\model\content\Category', 'id', 'category_id');
    }

    public function comment()
    {
        return $this->hasMany('app\common\model\content\Comment', 'content_id', 'id');
    }

    /**
     * User:Simony
     * Function: 列表
     * @param $condition
     * @param int $page
     * @param string $field
     * @param string $order
     * @return array
     * @throws \think\exception\DbException
     * @throws db\exception\DataNotFoundException
     * @throws db\exception\ModelNotFoundException
     */
    public function getContentList($condition = [], $category = 1, $order = 'id desc')
    {
        $type = $this->type;
        $data = Db::name($this->name)->where($condition)->order($order)->paginate()->each(function ($item, $key) use ($type, $category) {
            $item['apply_num'] = '';
            $item['nickname'] = '系统 & 管理员';
            if ($item['type'] == 2) {
                $item['apply_num'] = Db::name($this->name . '_comment')->where(['content_id' => $item['id'], 'type' => 2])->count();
            }
            if ($item['member_id'] > 0) {
                $nickname = Db::name('member')->where('id', $item['member_id'])->value('nickname');
                $item['nickname'] = empty($nickname) ? '--' : $nickname;
            }
            $item['location'] = empty($item['location']) ? '--' : $item['location'];
            $item['category_name'] = $category[$item['category_id']] ?? ' - ';
            $item['date'] = ($item['begin_date'] == $item['end_date']) ? $item['begin_date'] . '一整天' : $item['begin_date'] . ' - ' . $item['end_date'];
            $item['type'] = $type[$item['type']] ?? '';
            return $item;
        });
        return $data;
    }


    /**
     * User:Simony
     * Function: 获取内容列表
     * @param $where
     * @param $page
     * @param string $field
     * @param string $order
     * @return array|db\Query|\think\Paginator
     * @throws \think\exception\DbException
     */
    public function getPageContentList($where, $page, $field = '*', $order = 'c.id desc', $whereOr = [], $current_member_id = null)
    {
        $join_sql = model('common/member/File')->where(['type' => 6, 'status' => 1, 'member_id' => $current_member_id])->buildSql();
        $join_sql2 = model('common/member/FollowBlacklist')->where(['member_id' => $current_member_id])->buildSql();
        $res = $this->field($field)
            ->alias('c')
            ->join('content_category c_c', 'c_c.id=c.category_id', 'left')
            ->join('member m', 'm.id=c.member_id')
            ->join($join_sql2 . ' m_f_b', 'm_f_b.been_member_id=m.id', 'left');


        if (!is_null($current_member_id)) $res = $res->join($join_sql . ' f2', 'f2.table_id=c.id', 'left');
        $res = $res->with(['file' => function ($q) {
            $q->field('table_id,url,id')->where(['type' => 5]);
        }]);


        $res = $res->where($where);
        $res = $res->where([['c.audit_type','neq',3]]); //审核未通过的不显示
        $res = $res->where('m_f_b.type =1 OR m_f_b.type IS NULL');

        if ($whereOr) {
            $res = $res->where(function ($query) use ($whereOr) {
                $query->whereOr($whereOr);
            });
        }

        $res = $res->order($order)->paginate($page, false, ['query' => request()->param()]);
        return $res ? $res->toArray() : [];

    }


    /**
     * User: Simony
     * Function:今日已发内容数量
     * @param $member_id
     * @param $type 类型 0：系统广播、1：动态、2：节目
     */
    public function issuedTodayNum($member_id, $type = 1)
    {
        $where = [
            ['member_id', 'eq', $member_id],
            ['type', 'eq', $type], //动态
            ['is_pay', 'eq', 0],
            ['create_time', 'gt', strtotime(date('Y-m-d', time()))],
        ];

        return $this->getCount($where);
    }

}
